package com.seeyon.apps.kkARPEvent.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.common.po.filemanager.V3XFile;
import com.seeyon.ctp.common.screenshot.manager.ScreenShotManager;
import com.seeyon.ctp.util.HttpClientUtil;
import com.seeyon.ctp.util.SystemUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class FormToPdfUtils {
    public static ScreenShotManager screenShotManager = (ScreenShotManager) AppContext.getBean("screenShotManager");

    // 协同表单类型
    public static final int Form_TYPE_COL=1;

    //
    public static final int Form_TYPE_EDOC=2;
    public static V3XFile getV3XFile(int type,Long affirid,String memberId,boolean isShowComment,boolean isReplyContent,boolean autoFlag) {
        if(!autoFlag) {
            memberId=AppContext.getCurrentUser().getId().toString();
        }
       // reqOner();
        V3XFile file = null;
        ApplicationCategoryEnum appCategory = null;
        try {
            String url = null;
            if (Form_TYPE_COL == type) {
                url = "/ext/formCabinet.do?method=index&openFrom=listDone&affairId=" + affirid + "&isShowComment=" + isShowComment + "&isReplyContent=" + isReplyContent+ "&memberId=" + memberId;
                appCategory=ApplicationCategoryEnum.collaboration;
            } else if (Form_TYPE_EDOC == type) {
                url = "/ext/edocCabinet.do?method=index&openFrom=listDone&affairId=" + affirid + "&isShowComment=" + isShowComment + "&isReplyContent=" + isReplyContent+ "&memberId=" + memberId;
                appCategory=ApplicationCategoryEnum.govdoc;
            }
            ScreenShotManager.ScreenShotParam screenShotParam = new ScreenShotManager.ScreenShotParam();
            screenShotParam.setRenderType(ScreenShotManager.HtmlRenderType.pdf);
            //String base64 = screenShotManager.htmlToFile(url, screenShotParam);
            Map<String, Object> extParams = new HashMap<>();//封装PDF格式对象
            Map<String,Object> paperSize = new HashMap<>();//设置pdf纸张大小为A4
            paperSize.put("format", "A4");//设置页面布局为纵向
            paperSize.put("orientation", "portrait");//设置边距为0.8cm
            paperSize.put("margin", "0.8cm");
            extParams.put("paperSize", paperSize);//设置缩放比例
            extParams.put("zoomFactor", "1");
            screenShotParam.setExtParams(extParams);//生成pdf文件
            file = screenShotManager.htmlToPdf(url, screenShotParam, appCategory);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }


}
